package com.cloud.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory;
import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;


/**
 * @author root
 * @description TODO
 * @date 2020/11/26 10:23
 */
@Configuration
public class DataSourceConfig {

    //单个服务只应用单个表管理,不适合多个表处理
    @Bean
    public DataSource dataSource() throws SQLException {
        // 配置真实数据源
        Map<String, DataSource> dataSourceMap = new HashMap<>();

        // 配置第 1 个数据源
        DruidDataSource db1 = new DruidDataSource();
        db1.setUrl("jdbc:mysql://192.168.1.6:3306/ds0?characterEncoding=utf-8&serverTimezone=Asia/Shanghai");
        db1.setUsername("root");
        db1.setPassword("");
        dataSourceMap.put("ds0", db1);
        // 配置第 2 个数据源
        DruidDataSource db2 = new DruidDataSource();
        db2.setUrl("jdbc:mysql://192.168.1.6:3306/ds1?characterEncoding=utf-8&serverTimezone=Asia/Shanghai");
        db2.setUsername("root");
        db2.setPassword("");
        dataSourceMap.put("ds1", db2);

        Properties properties = new Properties();
        //sql显示
        properties.setProperty("sql-show", "true");
        // 创建 ShardingSphereDataSource 代理数据源
        return ShardingSphereDataSourceFactory.createDataSource(dataSourceMap, Collections.singleton((userShardingRuleConfiguration())), properties);
    }

    /**
     * @author lc
     * @date 2020/11/27
     * @description
     */
    private ShardingRuleConfiguration userShardingRuleConfiguration(){
        //根据user_id分库,再根据,order_id 分表
        ShardingTableRuleConfiguration userTbConfig = new ShardingTableRuleConfiguration("t_order_", "ds${0..1}.t_order_${0..1}");
        // 配置表字段分库策略
        userTbConfig.setDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("user_id", "dbShardingAlgorithm"));
        // 配置表字段分表策略
        userTbConfig.setTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "tableShardingAlgorithm"));
        // 配置分片规则
        ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
        shardingRuleConfig.getTables().add(userTbConfig);
        // 配置分库算法
        Properties db = new Properties();
        db.setProperty("algorithm-expression", "ds${user_id % 2}");
        shardingRuleConfig.getShardingAlgorithms().put("dbShardingAlgorithm", new ShardingSphereAlgorithmConfiguration("INLINE", db));
        // 配置分表算法
        Properties table = new Properties();
        table.setProperty("algorithm-expression", "t_order_${order_id % 2}");
        shardingRuleConfig.getShardingAlgorithms().put("tableShardingAlgorithm", new ShardingSphereAlgorithmConfiguration("INLINE", table));
        return shardingRuleConfig;
    }


}
